/**
 * 
 */
package mm.front.jsf.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import mm.back.entity.Vendedor;
import mm.back.service.VendedorService;

import org.springframework.web.jsf.FacesContextUtils;

/**
 * @author PUSDERKIS
 *
 */
public class VendedorConverter implements Converter{
	
	
	private VendedorService vendedorService = (VendedorService) FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean("vendedorService");

	/**
	 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		
		Vendedor v;
		
		if (value == null){
			return null;
		}
		if ("0".equals(value) || "seleccione un vendedor".equalsIgnoreCase(value)){
			return null;
		}
		v = vendedorService.getVendedor(Long.valueOf(value));
		return v;
	}

	/**
	 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		
		if (value != null && !"0".equals(value)){
			Vendedor v = (Vendedor) value;
			
			return v.getId() != null ? v.getId().toString() : null;
		}
		
		return null;
	}

}
